package com.test.demoapp.ui.query

import android.os.CountDownTimer
import android.widget.TextView
import com.test.demoapp.R
import com.test.demoapp.adapter.HisAdapter.Companion.transferLongToDate
import com.test.demoapp.base.BaseActivity
import com.test.demoapp.base.BaseFragment
import com.test.demoapp.entity.NewEvent
import com.test.demoapp.entity.RespVo
import com.test.demoapp.http.BaseURLConfig.KEY_HIS_DATA
import com.test.demoapp.http.BaseURLConfig.QUERY_URL
import com.test.demoapp.http.OkManager
import com.test.demoapp.util.GsonUtil
import com.test.demoapp.util.getSpListValue
import com.test.demoapp.util.putSpValue
import kotlinx.android.synthetic.main.fragment_home.*
import org.greenrobot.eventbus.EventBus
import java.lang.reflect.Field
import java.lang.reflect.Method
import java.util.*


class QueryFragment : BaseFragment() {

    private lateinit var timer: CountDownTimer

    override fun init() {
        timer = object : CountDownTimer(10000 * 10000, 5000) {
            override fun onFinish() {

            }

            override fun onTick(millisUntilFinished: Long) {
                getData()
            }
        }
        timer.start()
        var his = getSpListValue(key = KEY_HIS_DATA, cls = RespVo::class.java) as ArrayList
        if (his.size > 0) {
            changeUi(his[his.size-1])
        }
    }

    private fun getData() {
        OkManager.instance?.httpGet(QUERY_URL, object : OkManager.Fun1 {
            override fun onComplete(resp: OkManager.ResponseData) {
                var resp = GsonUtil.getObject(resp.response, RespVo::class.java)
                var his = getSpListValue(key = KEY_HIS_DATA, cls = RespVo::class.java) as ArrayList
                if (resp != null) {
                    resp.time = "" + System.currentTimeMillis()
                    his.add(resp);
                    putSpValue(key = KEY_HIS_DATA, value = his)
                    EventBus.getDefault().post(NewEvent())
                } else {
                    if (his.size > 0) {
                        resp = his[0]
                    }
                }
                if (resp != null) {
                    changeUi(resp)
                }
            }

        })
    }

    private fun changeUi(resp: RespVo) {
        ll_container?.removeAllViews()
        val field: Array<Field> = resp.javaClass.declaredFields

        field.forEachIndexed { index, it ->
            var name = it.name.substring(0, 1).toUpperCase() + it.name.substring(1);
            var childView = (activity as BaseActivity).makeView(R.layout.item_home)

            if(index==0){
                childView.findViewById<TextView>(R.id.tv_title).text = it.name+"      请求时间："+ resp.time?.toLong()?.let { time ->
                    transferLongToDate(
                        time
                    )
                }
            }else{
                childView.findViewById<TextView>(R.id.tv_title).text = it.name
            }
            val m: Method = resp.javaClass.getMethod("get$name")

            val value = m.invoke(resp) as String
            childView.findViewById<TextView>(R.id.tv_content).setText(value)
            ll_container?.addView(childView)
        }
    }


    override fun onPause() {
        super.onPause()
        if (timer != null) {
//            timer.cancel()
        }
    }

    override fun onResume() {
        super.onResume()
        if (timer != null) {
            timer.cancel()
            timer.start()
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
//        if (hidden) {
//            timer.cancel()
//        } else {
//            timer.cancel()
//            timer.start()
//        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (timer != null) {
            timer.cancel()
        }
    }

    override fun setLayoutId(): Int {
        return R.layout.fragment_home
    }
}
